const app = getApp()
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,//遮罩层
    time:0,
    topic_list:[],
    info:{},
    reply:'',
  },
  /**复盘考试 */
  seeRecord(e){
    wx.navigateTo({
      url: '../paperRecord/paperRecord?id='+e.currentTarget.dataset.id,
    })
  },
  /**继续考试 */
  continueExam(e){
    wx.navigateTo({
      url: '../exam/exam',
    })
  },
  /**上一题 下一题 */
  changeTopic(e){
    let type = e.currentTarget.dataset.type?e.currentTarget.dataset.type:'next';
    let that = this;
    let para = {
      paperId:this.data.info.id,
      type:type,
      list:this.data.info.list,
      reply:this.data.reply,
      topicId:this.data.topic_list[0].id
    };
    if(this.data.topic_list[0].topicType == '多选题'){
      para = {
        paperId:this.data.info.id,
        type:type,
        list:this.data.info.list,
        reply:JSON.stringify(this.data.reply),
        topicId:this.data.topic_list[0].id
      };
    }
   
    api.curlPost('?cla=paperManagerApp&fun=showNextTopic',para).then((res)=>{
      api.curlPost('?cla=paperManagerApp&fun=testPaper&list='+res.data.result.list,{id:res.data.result.id}).then((r)=>{
        that.setData({
          topic_list:r.data.result.topicItem,
          info:r.data.result,
          reply:r.data.result.topicItem[0].topicType=='多选题'?r.data.result.topicItem[0].reply:Number(r.data.result.topicItem[0].reply),
        })
      });
    });

  },
  /**交卷 */
  commit(){
    let that = this;
    let para = {
      paperId:this.data.info.id,
      reply:this.data.reply,
      topicId:this.data.topic_list[0].id
    };
    if(this.data.topic_list[0].topicType == '多选题'){
      para = {
        paperId:this.data.info.id,
        reply:JSON.stringify(this.data.reply),
        topicId:this.data.topic_list[0].id
      };
    }
    api.curlPost('?cla=paperManagerApp&fun=commitPaper',para).then((res)=>{
      api.curlPost('?cla=paperManagerApp&fun=completePage',{id:para.paperId}).then((back)=>{
        that.setData({
          show:true,
          complete:back.data.result
        })
      })
    });
  },
  /**倒计时结束 */
  finished(){
    this.commit()
  },
  /**单选框选择 */
  onRadioChange(event) {
    let radio =  event.detail;
    this.setData({
      reply:radio
    });
  },

  /**多选框 */
  onCheckBoxChange(event){
    let checked =  event.detail;
    this.setData({
      reply:checked
    });
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    api.curlPost('?cla=paperManagerApp&fun=testPaper',{id:options.id}).then((res)=>{
      console.log(res.data.result.topicItem[0].reply)
      that.setData({
        topic_list:res.data.result.topicItem,
        info:res.data.result,
        time:res.data.result.endTime*1000,
        reply:Number(res.data.result.topicItem[0].reply),
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})